package com.qianfeng.propertygroup.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.propertygroup.constants.ResultCode;
import com.qianfeng.propertygroup.dao.PublicboardDao;
import com.qianfeng.propertygroup.exceptions.*;
import com.qianfeng.propertygroup.pojo.Publicboard;
import com.qianfeng.propertygroup.service.intf.PublicboardService;
import com.qianfeng.propertygroup.vo.PublicboardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author LiXuekai on 2020/12/22
 */

@Service
public class PublicboardServiceImpl implements PublicboardService {
    @Autowired
    private PublicboardDao publicboardDao;
    @Override
    public void addPublicboard(Publicboard publicboard) {
        publicboard.setDate(new Date(System.currentTimeMillis()));
        try {
            publicboardDao.addPublicboard(publicboard);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddErrorException(e.getMessage(), ResultCode.DATA_NOT_ALLOWED);
        }
    }

    @Override
    public void updatePublicboard(Publicboard publicboard) {
        try {
            publicboardDao.updatePublicboard(publicboard);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpdateErrorException(e.getMessage(), ResultCode.DATA_NOT_ALLOWED);
        }
    }

    @Override
    public void deletePublicboard(Long id) {
        if (id == null) {
            throw new MyBaseException("id 不能为空", ResultCode.DATA_NOT_ALLOWED);
        }
        try {
            publicboardDao.deletePublicboard(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DeleteErrorException("没有传递 id",ResultCode.NULL_DATA);
        }

    }

    @Override
    public PageInfo<PublicboardVo> getAllPublicboard(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        try {
            PageInfo<PublicboardVo> pageInfo =new PageInfo<PublicboardVo>(publicboardDao.getAllPublicboard());
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new GetErrorException(e.getMessage(),ResultCode.NULL_DATA);
        }

    }

    @Override
    public PageInfo<Publicboard> getPublicboardBuDates(int pageNum, int pageSize,Date date) {
        PageHelper.startPage(pageNum,pageSize);
        try {
            PageInfo<Publicboard> pageInfo =new PageInfo<Publicboard>(publicboardDao.getPublicboardBuDates(date));
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new GetErrorException(e.getMessage(),ResultCode.NULL_DATA);
        }

    }
}
